package com.huilian.iotbox.admin.wap.service.impl;

import com.huilian.iotbox.admin.wap.dao.StatsLedgerMerchantDao;
import com.huilian.iotbox.admin.wap.service.StatsLedgerMerchantService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.StatsLedgerMerchantDto;
import com.huilian.iotbox.data.enums.LedgerOrderLedgerTypeEnum;
import com.huilian.iotbox.data.vo.StatsLedgerMerchantVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class StatsLedgerMerchantServiceImpl implements StatsLedgerMerchantService {
    @Resource
    private StatsLedgerMerchantDao statsLedgerMerchantDao;

    @Override
    public ServerResponse statsLedgerMerchantService(StatsLedgerMerchantDto statsLedgerMerchantDto) {
        statsLedgerMerchantDto.setLedgerType(LedgerOrderLedgerTypeEnum.GENERAL_LEDGER.getCode());
        List<StatsLedgerMerchantVo> statsLedgerMerchantVoList = statsLedgerMerchantDao.statsLedgerMerchantService(statsLedgerMerchantDto);
        List<StatsLedgerMerchantVo> arrays =
                statsLedgerMerchantVoList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
                        -> new TreeSet<>(Comparator.comparing(StatsLedgerMerchantVo::getDateStr))), ArrayList::new));
        List<StatsLedgerMerchantVo> list = new ArrayList<>();
        for (StatsLedgerMerchantVo smv1 : arrays) {
            StatsLedgerMerchantVo s = new StatsLedgerMerchantVo();
            List<StatsLedgerMerchantVo> listName = new ArrayList<>();
            BigDecimal money = BigDecimal.ZERO;
            s.setDateStr(smv1.getDateStr());
            for (StatsLedgerMerchantVo smv : statsLedgerMerchantVoList) {
                if (smv1.getDateStr().equals(smv.getDateStr()) && smv1.getMerchantStoreId().equals(smv.getMerchantStoreId())) {
                    money = money.add(smv.getMoney());
                    StatsLedgerMerchantVo statsLedgerMerchant = new StatsLedgerMerchantVo();
                    statsLedgerMerchant.setName(smv.getName());
                    statsLedgerMerchant.setMoney(smv.getMoney());
                    statsLedgerMerchant.setCostAmount(smv.getCostAmount());
                    s.setStoreName(smv.getStoreName());
                    listName.add(statsLedgerMerchant);
                }
            }
            s.setSName(listName);
            s.setDateMoneyTotal(money);
            list.add(s);
        }
        return ServerResponse.createBySuccess(list);
    }


    @Override
    public ServerResponse finStatsLedgerMerchantTotal(StatsLedgerMerchantDto statsLedgerMerchantDto) {
        statsLedgerMerchantDto.setLedgerType(LedgerOrderLedgerTypeEnum.GENERAL_LEDGER.getCode());
        StatsLedgerMerchantVo statsLedgerMerchantVo = statsLedgerMerchantDao.finStatsLedgerMerchantTotal(statsLedgerMerchantDto);
        return ServerResponse.createBySuccess(statsLedgerMerchantVo);
    }
}
